<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keywords" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordsQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="100" sortable>
      </el-table-column>
      <el-table-column prop="phone" label="电话号码" width="120" sortable>
      </el-table-column>
      <el-table-column prop="email" label="邮箱地址" width="150" sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="80" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100" sortable>
        <template scope="scope">
          <span style="color: #11b95c" v-if="scope.row.state==1">已启用</span>
          <span style="color: crimson" v-if="scope.row.state==0">未启用</span>
        </template>
      </el-table-column>
      <el-table-column prop="department.name" label="所属部门" width="120" sortable>
      </el-table-column>
      <el-table-column prop="shop_id" label="所属店铺" width="120" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="currentPage"
                     :page-size="pageSize" :total="totals" style="float:right;">
      </el-pagination>
    </el-col>

    <!-- 添加或修改的对话框
    el-dialog - elementUi提供的对话框 = 模态框
      :title="title" - 动态改变标题
      :visible.sync="saveFormVisible"  - 模态框的显示状态
      :close-on-click-modal="false" - 关闭模态框只能通过模态框的x关闭，点击空白处不能关闭模态框
    el-form - 表单
      :model="saveForm" - 绑定表单对象
      label-width="80px" - 标签宽度
      :rules="saveFormRules" - 验证规则
      ref="saveForm" - 为当前模态框取名
      prop="name" - 如果当前字段要验证，就必须写，出现*
      auto-complete="off" - 不自动提示
-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="saveForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="saveForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱地址">
          <el-input v-model="saveForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="saveForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="所属部门">
          <el-select v-model="saveForm.department_id" placeholder="请选择">
            <el-option
                v-for="item in departments"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.sn }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色">
          <!-- 员工可以选择多个角色：他们是多对多关系 - 所以有中间表 -->
          <!-- 这里我们设计的是一个员工选一个角色 -->
          <el-select v-model="saveForm.role_id" value-key="id" placeholder="请选择" clearable>
            <el-option
                v-for="item in roles"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.sn }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属店铺">
          <el-input v-model="saveForm.shop_id" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
export default {
  data() {
    return {
      filters: {
        keywords: ''
      },
      employees: [],
      totals: 0,
      currentPage: 1,
      pageSize: 3,
      listLoading: false,
      sels: [],//列表选中列

      saveFormVisible: false,//编辑界面是否显示
      saveLoading: false,
      title: '',
      saveFormRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ],
        sn: [
          {required: true, message: '请输入编号', trigger: 'blur'}
        ]
      },
      // 添加或界面数据
      saveForm: {
        id: null,
        username: '',
        phone: '',
        email: '',
        salt: '',
        password: '',
        age: null,
        state: 1,
        role_id: null,
        department_id: null,
        logininfo_id: null,
        shop_id: null
      },
      departments: [],
      roles: []
    }
  },
  methods: {
    keywordsQuery() {
      this.currentPage = 1;
      this.getEmployees();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getEmployees();
    },
    //获取用户列表
    getEmployees() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keywords: this.filters.keywords
      };
      this.listLoading = true;
      this.$http.post("/employee", para).then(result => {
        this.totals = result.data.totals;
        this.employees = result.data.data;
        this.listLoading = false;
      }).catch(result => {
        this.$message.error('请求错误');
      })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/employee/" + row.id).then(res => {
          this.$message(res.data.msg);
        }).catch(res => {
          this.$message({
            message: '系统错误，稍后尝试',
            type: 'error'
          });
        })
        this.currentPage = 1;
        this.getEmployees();
        this.listLoading = false;
      }).catch(() => {
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.title = '编辑';
      this.getDepartments();
      this.getRoles();
      this.saveForm = Object.assign({}, row);
      this.saveFormVisible = true;
    },
    //显示新增界面
    handleAdd: function () {
      this.title = '新增';
      this.getDepartments();
      this.getRoles();
      this.saveForm = {
        id: null,
        username: '',
        phone: '',
        email: '',
        salt: '',
        password: '',
        age: null,
        state: 1,
        department_id: null,
        logininfo_id: null,
        shop_id: null
      };
      this.saveFormVisible = true;
    },
    // 提叫删除或修改
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            let para = Object.assign({}, this.saveForm);
            this.$http.put("/employee/", para).then(res => {
              this.$message(res.data.msg);
            }).catch(res => {
              this.$message({
                message: '系统错误，稍后尝试',
                type: 'error'
              });
            });
            this.saveLoading = false;
            this.saveFormVisible = false;
            this.getEmployees();
          });
        }
      });
    },
    // 批量选择时参数
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/employee/", ids).then(res => {
          this.$message(res.data.msg);
        }).catch(res => {
          this.$message({
            message: '系统错误，稍后尝试',
            type: 'error'
          });
        })
        this.currentPage = 1;
        this.listLoading = false;
        this.getEmployees();
      }).catch(() => {

      });
    },
    getDepartments() {
      this.$http.get("/department").then(res => {
        this.departments = res.data;
      })
    },
    getRoles() {
      this.$http.get("/role").then(res => {
        this.roles = res.data;
      })
    }
  },
  mounted() {
    this.getEmployees();
  }
}
</script>

<style scoped>

</style>